package com.software.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.software.bean.CustomerAddressBean;
import com.software.dto.AddAddressDto;
import com.software.dto.UpdateAddressDto;
import com.software.mapper.CustomerAddressMapper;
import com.software.service.CustomerAddressService;
import com.software.utils.AssertUtil;
import com.software.utils.PhoneUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wyj
 * @since 2024-03-31
 */
@Service
public class CustomerAddressServiceImpl extends ServiceImpl<CustomerAddressMapper, CustomerAddressBean> implements CustomerAddressService {


    @Autowired
    private CustomerAddressMapper customerAddressMapper;


    /**
     * @description:添加地址
     * @author: wyj
     * @date: 2024/3/31 19:36
     * @param:
     * @return:
     */
    @Override
    public Integer addAddress(AddAddressDto addAddressDto) {
        //参数校验
        AssertUtil.isTrue(addAddressDto.getCid() == null, "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(addAddressDto.getProvince()), "省份不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(addAddressDto.getCity()), "市不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(addAddressDto.getDistrict()), "区不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(addAddressDto.getDetailAddress()), "详细地址不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(addAddressDto.getPhone()), "手机号不能为空");
        AssertUtil.isTrue(PhoneUtils.isMobile(addAddressDto.getPhone()) == false, "手机号格式不符合");
        AssertUtil.isTrue(addAddressDto.getIsDefault() == null, "参数异常");
        CustomerAddressBean insertAddressBean = getCustomerAddressBean(addAddressDto);
        //如果添加的地址是默认 修改所有的这个用户的地址不默认 默认1 不默认0
        //判断地址表是否有该用户的默认数据
        CustomerAddressBean selectAddressBeanForUpdate = customerAddressMapper.selectAddressByCidForIsDefault(addAddressDto.getCid());
        // 如果数据库没有地址数据  并且插入的是默认数据
        if (selectAddressBeanForUpdate == null && insertAddressBean.getIsDefault() == 1) {
            // 直接插入
            Integer result = customerAddressMapper.insert(insertAddressBean);
            return result == 1 ? 1 : 0;
        } else if (selectAddressBeanForUpdate != null && insertAddressBean.getIsDefault() == 0) {
            //如果有默认地址  并且插入的不是默认地址 那就直接插入
            Integer i = customerAddressMapper.insert(insertAddressBean);
            return i == 1 ? 1 : 0;
        } else if (selectAddressBeanForUpdate != null && selectAddressBeanForUpdate.getIsDefault() == 1 && insertAddressBean.getIsDefault() == 1) {
            //如果有默认地址  并且要将新插入改成默认地址 那么先更新在插入
            selectAddressBeanForUpdate.setIsDefault(0);
            int i = customerAddressMapper.updateById(selectAddressBeanForUpdate);
            AssertUtil.isTrue(i != 1, "参数异常");
            Integer res = customerAddressMapper.insert(insertAddressBean);
            return res == 1 ? 1 : 0;
        }
        return 0;
    }

    /**
     * @description:全部地址列表
     * @author: wyj
     * @date: 2024/3/31 20:56
     * @param:
     * @return:
     */
    @Override
    public List<CustomerAddressBean> addressList(Integer cid) {
        AssertUtil.isTrue(cid == null, "参数异常");
        List<CustomerAddressBean> list = customerAddressMapper.selectAddressListByCid(cid);
        return list;
    }

    /**
     * @description:根据addressId查addressBean作用是编辑地址的数据回显
     * @author: wyj
     * @date: 2024/4/1 0:04
     * @param:
     * @return:addressBean
     */
    @Override
    public CustomerAddressBean getAddressByAddressId(Integer aId) {
        AssertUtil.isTrue(aId == null, "参数异常");
        CustomerAddressBean customerAddressBean = customerAddressMapper.selectById(aId);
        AssertUtil.isTrue(customerAddressBean == null, "返回值异常");
        return customerAddressBean;
    }

    /**
     * @description：更新地址
     * @author: wyj
     * @date: 2024/4/1 13:47
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public Integer updateAddress(UpdateAddressDto updateAddressDto) {
        //参数校验
        AssertUtil.isTrue(updateAddressDto.getId() == null, "参数异常");
        AssertUtil.isTrue(updateAddressDto.getCustomerId() == null, "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updateAddressDto.getDetailAddress()), "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updateAddressDto.getProvince()), "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updateAddressDto.getCity()), "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updateAddressDto.getDistrict()), "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updateAddressDto.getNickName()), "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(updateAddressDto.getPhone()), "参数异常");
        AssertUtil.isTrue(PhoneUtils.isMobile(updateAddressDto.getPhone()) == false, "手机号格式不符合");
        AssertUtil.isTrue(updateAddressDto.getIsDefault() == null, "参数异常");
        CustomerAddressBean customerAddressBean = getCustomerAddressBean(updateAddressDto);
        CustomerAddressBean addressBeanIsDefault = customerAddressMapper.selectAddressByCidForIsDefault(customerAddressBean.getCustomerId());
        System.out.println(addressBeanIsDefault);
        // 判断是否默认
        // =0更新后的地址不是默认 那么直接更新即可
        if (customerAddressBean.getIsDefault() == 0) {
            int i = customerAddressMapper.updateById(customerAddressBean);
            return i == 1 ? 1 : 0;
        } else if (customerAddressBean.getIsDefault() == 1 && addressBeanIsDefault != null) {
            // 更新后地址是默认 并且有默认地址
            //先将原本默认的更新成不是默认 在更新新 地址
            addressBeanIsDefault.setIsDefault(0);
            int i = customerAddressMapper.updateById(addressBeanIsDefault);
            AssertUtil.isTrue(i != 1, "更新失败");
            int result = customerAddressMapper.updateById(customerAddressBean);
            return result == 1 ? 1 : 0;
        } else if (customerAddressBean.getIsDefault() == 1 && addressBeanIsDefault == null) {
            // 更新后的地址是默认  此时没有默认地址 那么直接更新
            System.out.println(123456);
            int result = customerAddressMapper.updateById(customerAddressBean);
            return result == 1 ? 1 : 0;
        }
        return 0;

    }

    /**
     * @description:删除地址
     * @author: wyj
     * @date: 2024/4/1 13:54
     * @param:
     * @return:
     */
    @Override
    public Integer deleteAddressById(Integer id) {
        // 参数校验
        AssertUtil.isTrue(id == null, "参数异常");
        //判断id是否合法
        CustomerAddressBean customerAddressBean = customerAddressMapper.selectById(id);
        AssertUtil.isTrue(customerAddressBean == null, "返回值异常");
        // 逻辑删除 isDelete = 1
        customerAddressBean.setIsDelete(1);
        int i = customerAddressMapper.updateById(customerAddressBean);
        return i == 1 ? 1 : 0;
    }

    private CustomerAddressBean getCustomerAddressBean(UpdateAddressDto updateAddressDto) {
        CustomerAddressBean customerAddressBean = new CustomerAddressBean();
        customerAddressBean.setCustomerId(updateAddressDto.getCustomerId());
        customerAddressBean.setDetailAddress(updateAddressDto.getDetailAddress());
        customerAddressBean.setProvince(updateAddressDto.getProvince());
        customerAddressBean.setCity(updateAddressDto.getCity());
        customerAddressBean.setDistrict(updateAddressDto.getDistrict());
        customerAddressBean.setIsDefault(updateAddressDto.getIsDefault());
        customerAddressBean.setCustomerNickName(updateAddressDto.getNickName());
        customerAddressBean.setCustomerPhone(updateAddressDto.getPhone());
        customerAddressBean.setId(updateAddressDto.getId());
        return customerAddressBean;
    }

    private CustomerAddressBean getCustomerAddressBean(AddAddressDto addAddressDto) {
        CustomerAddressBean customerAddressBean = new CustomerAddressBean();
        customerAddressBean.setCustomerId(addAddressDto.getCid());
        customerAddressBean.setDetailAddress(addAddressDto.getDetailAddress());
        customerAddressBean.setProvince(addAddressDto.getProvince());
        customerAddressBean.setCity(addAddressDto.getCity());
        customerAddressBean.setDistrict(addAddressDto.getDistrict());
        customerAddressBean.setIsDefault(addAddressDto.getIsDefault());
        customerAddressBean.setCustomerNickName(addAddressDto.getNickName());
        customerAddressBean.setCustomerPhone(addAddressDto.getPhone());
        customerAddressBean.setIsDelete(0);
        return customerAddressBean;
    }
}
